<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
echo "<response>";

session_start();
if ($_SESSION['agent_auth'] != true && $_SESSION['kteam_auth'] != true)
{
    $expire=time()+60*60;
    setcookie("redirector", "customer.html", $expire);
    echo "<result>Login</result>";
    echo "</response>";
    //header("Location: login.html");
    exit;
}

include("sqlconnect.php");
include("sqlgenerator.php");
include("storeticket.php");
$debug = false;

if ($debug) {
    echo '<debug>';
    echo 'Post parameters: ' . $_GET['cust_name'];
    echo '</debug>';
}

$fields[0] = $_GET['cust_name'];
$fields[1] = $_GET['centre_code'];
$fields[2] = $_GET['agent_code'];
$fields[3] = $_GET['film_code'];
$fields[4] = $_GET['phone'];
$fields[5] = $_GET['ticket_no'];
$fields[6] = $_GET['date_issue'];
$fields[7] = $_GET['time_issue'];
$fields[8] = $_GET['ticket_rate'];
$fields[9] = 100;

db_connect();

$query = get_ticketinfo($fields[8]);
if ($debug) {
    echo '<debug>';
    echo 'Query: ' . $query;
    echo '</debug>';
}
$result = db_query($query);
$row = mysql_fetch_array($result);
try {
    $fields[9] = $row['coupon_value'];

} catch (Exception $e) {
    switch($fields[8]) {
        case '40':
            $fields[9] = 80;
            break;
        case '50':
            $fields[9] = 100;
            break;
        default:
            $fields[9] = 80;
            break;
    }

}

// Check if the the customer is already in customer table
$query = get_customer($fields[5]);
if ($debug) {
    echo '<debug>';
    echo 'Query: ' . $query;
    echo '</debug>';
}

$result = db_query($query);

if ($debug) {
    echo '<debug>';
    echo 'Query Result: ' .$resut;
    echo '</debug>';
}

if(mysql_num_rows($result) != 0) {
    // Customer exists already so update the same
    $row = mysql_fetch_array($result);
    $query = update_customer($row['id'], $fields);
    if ($debug) {
        echo '<debug>';
        echo 'Query: ' . $query;
        echo '</debug>';
    }

    $result = db_query($query);

    if ($debug) {
        echo '<debug>';
        echo 'Query Result: ' .$resut;
        echo '</debug>';
    }
    echo "<result>Updated</result>";
} else {
    // Add the customer here
    $query = insert_customer($fields);
    if ($debug) {
        echo '<debug>';
        echo 'Query: ' . $query;
        echo '</debug>';
    }

    $result = db_query($query);

    if ($debug) {
        echo '<debug>';
        echo 'Query Result: ' .$resut;
        echo '</debug>';
    }

    store_ticket($fields[5], $fields[8], $fields[9]);
    echo "<result>Added</result>";
}

try {
    $film_code = $fields[3];
    $query = get_film($film_code);
    echo $query;
    $result = db_query($query);

    $sold = $row['sold_no_tickets'];
    $sold++;

    $query = update_film_sold_tickets($film_code, $sold);
    echo $query;
    $result = db_query($query);
    echo "<error>Updated sold tickets</error>";
} catch (Exception $e) {
    echo "<error>Could not update sold tickets</error>";
}


echo "</response>";

?>